package cn.edu.sjtu.software.sjtucontacts.activity;

import java.io.File;

import cn.edu.sjtu.software.sjtucontacts.R;
import cn.edu.sjtu.software.sjtucontacts.util.ExpBaseAdapter;
import cn.edu.sjtu.software.sjtucontacts.util.FileData;
import cn.edu.sjtu.software.sjtucontacts.util.StringUtil;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class FrmOpenfile extends Activity implements OnClickListener {
	ListView lv2;
	private File mCurrentDirectory = new File("/sdcard/");
	ExpBaseAdapter ap;
	String fileEndings[] = { "xml","xls","xlsx" };
	Button confirmButton,cancelButton;
	EditText saveName;
	String directory=null;
	String fileName=null;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mCurrentDirectory = new File(new StringUtil().getSDPath()+"/");
		directory = new StringUtil().getSDPath()+"/";
		
		setTitle(R.string.text_selectfile);
		setContentView(R.layout.frm_openfile);
		lv2 = (ListView) findViewById(R.id.of_lv);
		lv2.setCacheColorHint(0x00000000);
		ap = new ExpBaseAdapter(this);
		lv2.setAdapter(ap);
		ListView.OnItemClickListener lv2click = new ListView.OnItemClickListener() {
			public void onItemClick(AdapterView parent, View view,
					int position, long id) {
				// "_id", "ext_number", "name","ann", "intro"
				int fid = ap.getItemType((int) id);
				String mPath = "";
				if (fid == 1) {
					String s1 = ap.getItem((int) id).name;
					if (s1.equals("..")) {
						mPath = mCurrentDirectory.getParent();
					} else {
						mPath = mCurrentDirectory.getPath() + "/" + s1 + "/";
					}
					mCurrentDirectory = new File(mPath);
					ListFile(mCurrentDirectory);
					setCurrentDirectory(mPath);
				} else {
					/*
					Bundle bundle = new Bundle();
					bundle.putString("filename", mCurrentDirectory.getPath()
							+ "/" + ap.getItem((int) id).name);
					Intent mIntent = new Intent();
					mIntent.putExtras(bundle);
					setResult(RESULT_OK, mIntent);
					FrmOpenfile.this.finish();
					*/
					saveName.setText(ap.getItem((int) id).name);
				}
			}
		};
		ListFile(mCurrentDirectory);
		lv2.setOnItemClickListener(lv2click);
		
		saveName = (EditText)this.findViewById(R.id.save_file_path);
		
		confirmButton = (Button)this.findViewById(R.id.open_file_confirmbutton);
		cancelButton = (Button)this.findViewById(R.id.open_file_cancelButton);
		
		confirmButton.setOnClickListener(this);
		cancelButton.setOnClickListener(this);
	}

	private void ListFile(File aDirectory) {
		ap.clearItems();
		ap.notifyDataSetChanged();
		lv2.postInvalidate();
		Log.v("vodone", "mpath=" + aDirectory.getPath());
		// if (aDirectory==null)return;
		if (!aDirectory.getPath().equals("/sdcard")) {
			FileData fd = new FileData();
			fd.name = "..";
			fd.type = 1;
			ap.addItem(fd);
		}
		for (File f : aDirectory.listFiles()) {
			if (f.isDirectory()) {
				FileData fd = new FileData();
				fd.name = f.getName();
				fd.type = 1;
				ap.addItem(fd);
			} else {
				if (checkEnds(f.getName().toLowerCase())) {
					FileData fd = new FileData();
					fd.name = f.getName();
					fd.type = 0;
					ap.addItem(fd);
				}
			}
		}
		ap.notifyDataSetChanged();
		lv2.postInvalidate();
	}

	private boolean checkEnds(String checkItsEnd) {
		for (String aEnd : fileEndings) {
			if (checkItsEnd.endsWith(aEnd))
				return true;
		}
		return false;
	}

	@Override
	public void onClick(View v) {
		if(v==confirmButton){
			Intent result = new Intent("RETURN_FILE_PATH", null);
			fileName = saveName.getText().toString();
			if(fileName==null||fileName.equals("")){
				return;
			}else if(!fileName.endsWith(".xls")&&!fileName.endsWith(".xlsx")){
				fileName = fileName+".xls";
			}
			result.putExtra("directory_path", directory);
			result.putExtra("file_path", fileName);
			
			setResult(RESULT_OK, result);
			finish();
		}else if(v==cancelButton){
			setResult(RESULT_CANCELED, null);
			finish();
		}

	}
	
	public void setCurrentDirectory(String dir){
		this.directory = dir;
	}
}
